package com.znk.aop;

import org.springframework.aop.framework.AopProxy;

import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

public class MyJdkDynamicProxy_02 implements AopProxy, InvocationHandler, Serializable {

    private Object o = null;

    public Object bind (Object o) {
        this.o = o;
        return this.getProxy(MyJdkDynamicProxy_02.class.getClassLoader());
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        String name = method.getName();
        System.out.println(name + "方法的参数是：" + Arrays.asList(args));
        return method.invoke(o, args);
    }

    @Override
    public Object getProxy() {
        return null;
    }

    @Override
    public Object getProxy(ClassLoader classLoader) {
        return Proxy.newProxyInstance(classLoader, o.getClass().getInterfaces(), this);
    }
}
